package com.sky.controller.admin;

import com.sky.utils.AliOssUtil;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Api(tags = "通用接口")
@RestController
@Slf4j
@RequestMapping("/admin/common")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        //获取文件名
        String originalFilename = file.getOriginalFilename();
        //构造新的文件名,后缀名
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + substring;

        String upload = "";
        try {
            upload = aliOssUtil.upload(file.getBytes(), newFileName);
            log.info("文件上传成功" + newFileName);
            return Result.success(upload);
        } catch (IOException e) {
            log.info("文件上传失败");
            e.printStackTrace();
        }
        return Result.error("文件上传失败");

    }
}
